import './session-storage'

export default {

    /**
     * 空校验
     * @param {*} obj 
     * @returns 
     */
    isEmpty: function(obj) {
        if((typeof obj) == 'string') {
            return !obj || obj.replace(/\s+/g, "") == ""
        } else {
            return (!obj || JSON.stringify(obj) === "{}" || obj.length == 0)
        }
    },

    /**
     * 非空校验
     * @param {*} obj 
     * @returns 
     */
    isNotEmpty: function(obj) {
        return !this.isEmpty(obj)
    },

    /**
     * 长度校验
     * @param {*} str 
     * @param {*} min 
     * @param {*} max 
     */
    isLength: function(str, min, max) {
        return str.trim().length >= min && str.trim().length <= max
    },

 /**
   * 移除对象数组中的对象
   * @param array
   * @param obj
   * @returns {number}
   */
  removeObj: function (array, obj) {
    let index = -1;
    for (let i = 0; i < array.length; i++) {
      if (array[i] === obj) {
        array.splice(i, 1);
        index = i;
        break;
      }
    }
    return index;
  },
    
  
  /**
   * 10进制转62进制
   * @param number
   * @returns {string}
   * @private
   */
   _10to62: function (number) {
    let chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ';
    let radix = chars.length;
    let arr = [];
    do {
      let mod = number % radix;
      number = (number - mod) / radix;
      arr.unshift(chars[mod]);
    } while (number);
    return arr.join('');
  },

  /**
 * 查找是否有权限
 * @param id 资源id
 */
    hasResources: function (id) {
    let _this = this;
    let resources = SessionStorage.get(SESSION_KEY_USER).resources
    if (_this.isEmpty(resources)) {
      return false;
    }
    for (let i = 0; i < resources.length; i++) {
      if (id === resources[i].id) {
        return true;
      }
    }
    return false;
  }

}